package com.eischet.lookup.web;

/**
 * This enum defines the different pages that Lookup can serve.
 */
public enum Pages {
    SearchForm("f"),
    Results("r"),
    About("a"),
    Help("h"),
    RawView("w");

    private final String code;

    /**
     * Create a Pages instance with a code; this code is the designated value of an HTTP request parameter.
     * @param code  the page's code
     */
    private Pages(String code) {
        this.code = code;
    }

    /**
     * @return the page's code
     */
    private String getCode() {
        return code;
    }

    /**
     * return the Pages enum value that has the given code from an HTTP request parameter.
     * @param doArg     the request parameter value
     * @return          the Page, or the SearchForm as a default
     */
    public static Pages getPageFor(String doArg) {
        for (Pages page: values()) {
            if (page.getCode().equalsIgnoreCase(doArg)) {
                return page;
            }
        }
        return SearchForm;
    }

    /**
     * @return a String representation of the Pages object
     */
    @Override
    public String toString() {
        return String.format("Pages(code='%s')", code);
    }
}
